<?php
/**
 * 门店登录控制器
 * Created by PhpStorm.
 * Author: 芸众商城 www.yunzshop.com
 * Date: 2024/1/1
 * Time: 上午10:00
 */

namespace App\Http\Controllers\Api\store;

use App\Common\Components\ApiController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Api\BaseApiController;

class StoreLoginController extends BaseApiController
{
    protected $publicController = ['StoreLogin'];
    protected $publicAction = ['login'];
    protected $ignoreAction = ['login'];

    /**
     * 门店登录接口
     */
    public function login()
    {
        $username = request()->input('param.username');
        $password = request()->input('param.password');
        
        if (empty($username)) {
            return $this->errorJson('用户名不能为空');
        }
        
        if (empty($password)) {
            return $this->errorJson('密码不能为空');
        }
        
        // 查询管理员用户信息
        $adminUser = DB::table('ims_yz_admin_users')
            ->where('username', $username)
            ->where('status', 2) // 状态为有效
            ->where('type', 3)   // 类型为店员
            ->first();
        
        if (!$adminUser) {
            return $this->errorJson('用户不存在或已被禁用');
        }
        
        // 验证密码 - 使用bcrypt方式
        if (!Hash::check($password, $adminUser->password)) {
            return $this->errorJson('密码错误');
        }
        
        // 查询门店信息
        $store = DB::table('ims_yz_store')
            ->where('user_uid', $adminUser->uid)
            ->first();
        
        if (!$store) {
            return $this->errorJson('该用户未绑定门店');
        }
        
        // 更新最后登录时间和IP
        DB::table('ims_yz_admin_users')
            ->where('uid', $adminUser->uid)
            ->update([
                'lastvisit' => time(),
                'lastip' => request()->getClientIp(),
                'updated_at' => time()
            ]);
        
        // 准备返回数据
        $userInfo = [
            'uid' => $adminUser->uid,
            'username' => $adminUser->username,
            'status' => $adminUser->status,
            'type' => $adminUser->type,
            'remark' => $adminUser->remark,
            'lastvisit' => $adminUser->lastvisit,
            'lastip' => $adminUser->lastip,
            'created_at' => $adminUser->created_at,
            'updated_at' => $adminUser->updated_at
        ];
        
        $storeInfo = [
            'id' => $store->id,
            'uniacid' => $store->uniacid,
            'uid' => $store->uid,
            'store_name' => $store->store_name,
            'thumb' => $store->thumb,
            'category_id' => $store->category_id,
            'province_id' => $store->province_id,
            'city_id' => $store->city_id,
            'district_id' => $store->district_id,
            'street_id' => $store->street_id,
            'address' => $store->address,
            'longitude' => $store->longitude,
            'latitude' => $store->latitude,
            'mobile' => $store->mobile,
            'store_introduce' => $store->store_introduce,
            'username' => $store->username,
            'template_id' => $store->template_id,
            'business_hours_start' => $store->business_hours_start,
            'business_hours_end' => $store->business_hours_end,
            'dispatch_type' => $store->dispatch_type,
            'affiche' => $store->affiche,
            'user_uid' => $store->user_uid,
            'information' => $store->information,
            'is_black' => $store->is_black,
            'banner_thumb' => $store->banner_thumb,
            'is_hide' => $store->is_hide,
            'specify_show' => $store->specify_show,
            'operating_state' => $store->operating_state,
            'boss_uid' => $store->boss_uid,
            'audio_open' => $store->audio_open,
            'initials' => $store->initials,
            'validity' => $store->validity,
            'validity_status' => $store->validity_status,
            'yun_horn' => $store->yun_horn,
            'zkc_pay' => $store->zkc_pay,
            'home_store_url' => $store->home_store_url,
            'goods_store_url' => $store->goods_store_url,
            'collect_money_url' => $store->collect_money_url,
            'payment_jump_minapp' => $store->payment_jump_minapp,
            'payment_jump_h5' => $store->payment_jump_h5,
            'is_open_jump' => $store->is_open_jump,
            'separate_payment_jump_minapp' => $store->separate_payment_jump_minapp,
            'separate_payment_jump_h5' => $store->separate_payment_jump_h5,
            'balance_id' => $store->balance_id,
            'platform_ids' => $store->platform_ids,
            'settlement_num' => $store->settlement_num,
            'settlement_type' => $store->settlement_type,
            'recruiter_id' => $store->recruiter_id,
            'platform_id' => $store->platform_id,
            'created_at' => $store->created_at,
            'updated_at' => $store->updated_at
        ];
        
        $data = [
            'user' => $userInfo,
            'store' => $storeInfo
        ];
        
        return $this->successJson('登录成功', $data);
    }
}
